// single-spa 在 webpack 打包的时候已经放到 externals，需要在模板引入
import { registerApplication, start } from "single-spa";
import { constructApplications, constructRoutes } from "single-spa-layout"

// 1、Layout 引擎配置：

// 获取路由配置对象：获取 template 里面的路由
const routes = constructRoutes(document.querySelector("#single-spa-layout"))
// 获取路由信息数组
const applications = constructApplications({
  routes,
  loadApp({ name }) {
    return System.import(name)
  }
})
// 遍历路由信息注册应用
applications.forEach(registerApplication)


// 2、普通配置：

// // 配置参考：https://zh-hans.single-spa.js.org/docs/api
// registerApplication({
//   name: "@myspa/welcome",
//   app: () =>
//     System.import(
//       "@myspa/welcome"
//     ),
//   // 严格匹配采用这种方式
//   activeWhen: location => location.pathname === "/"
// });

// registerApplication({
//   name: "@myspa/sample",
//   app: () => System.import("@myspa/sample"),
//   activeWhen: "/sample"
// });

// registerApplication({
//   name: "@myspa/react",
//   app: () => System.import("@myspa/react"),
//   activeWhen: "/react"
// });

// registerApplication({
//   name: "@myspa/vue",
//   app: () => System.import("@myspa/vue"),
//   activeWhen: "/vue"
// });

start({
  urlRerouteOnly: true,
});

// urlRerouteOnly: A boolean that defaults to false. If set to true, calls to history.pushState() and history.replaceState() will not trigger a single-spa reroute unless the client side route was changed. Setting this to true can be better for performance in some situations. For more information, read original issue.